<template>
  <div>
    <el-card style="margin: 10px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>扶贫政策</el-breadcrumb-item>
        <el-breadcrumb-item>扶贫政策管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input style="top: 20px" placeholder="请输入内容" v-model="type" :clearable="true">
            <el-button slot="append" class="el-icon-search" @click="getList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" class="btn" @click="fromClick"
            >添加政策</el-button
          >
           <el-button type="success" class="btn" @click="addSearch"
              >更多搜索</el-button
            >
        </el-col>
      </el-row>
      <el-row :gutter="12" v-show="Search" style="margin:10px -5px">
          <el-col :span="8"
            ><el-input
              style="top: 20px"
              placeholder="请输入id"
              v-model="id"
              :clearable="true"
              @click="getList"
            >
              <el-button
                slot="append"
                class="el-icon-search"
                @click="getList"
              ></el-button> </el-input
          ></el-col>
          <el-col :span="8"
            ><el-input
              style="top: 20px"
              placeholder="请输入扶贫类型"
              v-model="title"
              :clearable="true"
              @click="getList"
            >
              <el-button
                slot="append"
                class="el-icon-search"
                @click="getList"
              ></el-button> </el-input
          ></el-col>
        </el-row>
    </el-card>
    <el-card>
      <el-table :data="tableData">
        <el-table-column prop="id" label="ID  "> </el-table-column>
        <el-table-column prop="title" label="扶贫类型">
        </el-table-column>
        <el-table-column prop="type" show-overflow-tooltip label="内容">
        </el-table-column>
        <el-table-column prop="cover" label="封面">
          <template slot-scope="scope">
            <img width="100px" height="100px" :src="scope.row.cover"/>
          </template>
        </el-table-column>
        <el-table-column prop="clickNum" label="点击次数"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column prop="content" label="详细介绍" :show-overflow-tooltip="true"> </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="userFrom(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="removeUser(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          layout="prev, pager, next"
          :total= total
          :page-size = pageSize
          @current-change="change"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 
      打开添加框
     -->
    <el-dialog title="添加信息" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="formAdd">
        <el-form-item label="id">
          <el-input v-model="formAdd.id"></el-input>
        </el-form-item> 
        <el-form-item label="创建时间">
          <el-input v-model="formAdd.create_time"></el-input>
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="formAdd.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="formAdd.content"></el-input>
        </el-form-item>
        <el-form-item label="图片">
            <el-upload
              class="upload-demo"
              multiple
              accept="image/jpeg,image/png,image/jpg"
              :limit="1"
              action="action"
              :http-request="uploadFile"
              :auto-upload="true"
              ref="upload"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-form-item>
      </el-form>
      <div class="dialog-footer" style="top: 10px">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onFrom">确 定</el-button>
      </div>
    </el-dialog>

    <!--点击更改-->
    <el-dialog
      title="更改信息"
      :visible.sync="dialogFormVisibleInfo"
      width="500px"
    >
      <el-form :model="fromUpdateUser">
        <el-form-item label="标题">
          <el-input v-model="fromUpdateUser.title"></el-input>
        </el-form-item>
        <el-form-item label="扶贫类型">
          <el-input v-model="fromUpdateUser.type"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="fromUpdateUser.content"></el-input>
        </el-form-item>
        <el-form-item label="ID">
          <el-input v-model="fromUpdateUser.id"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" style="top: 10px">
        <el-button @click="dialogFormVisibleInfo = false">取 消</el-button>
        <el-button type="primary" @click="onUpdateUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "fupin",
  data() {
    return {
      formAdd: {
        id: "0",
        create_time: "",
        title: "",
        content: "",
        cover: "",
      },
      fromUpdateUser: {
        title: "",
        type: "",
        content: "",
        id: "",
      },
      tableData: [],
      pageSize: 3, 
      pageNum: 1,
      dialogFormVisible: false,
      dialogFormVisibleInfo: false,
      type: "",
      total:null,
      imgUrl: "",
      Search:'',
      id: "",
      title: "",
    };
  },
  methods: {
    async getList() {
      let { data: res } = await this.$axios.get("/alleviation/getlist", {
        params: {
          pageSize:this.pageSize,
          pageNum:this.pageNum,
          type: this.type,
          id: this.id,
          title: this.title,
        },
      });
      this.tableData = res.data.list;
      this.total = res.data.total
      // this.pageNum = res.data.pageNum
    },
    // 当前页发生改变时,触发该函数
    change(e) {
      this.pageNum = e;
      this.getList(this.pageSize, e);
    },
    fromClick() {
      this.dialogFormVisible = true;
      let time = this.nowTime();
      this.formAdd.create_time = time;
    },

    // 上传图片
    async uploadFile() {
      const fromData = new FormData();
      const file = this.$refs.upload.uploadFiles[0];
      const headerConfig = {
        headers: { "Content-Type": "multipart/from-data" },
      };
      if (!file) {
        alert("请选择文件");
        return;
      }
      // console.log("图片解析为" + file.raw);
      fromData.append("file", file.raw);
      fromData.append("type", "user");
      let { data: res } = await this.$axios.post("/uploadImg",fromData,headerConfig);
      // console.log(res);
      this.imgUrl = res.data.imgurl;
    },

    async onFrom() {
      this.formAdd.cover = this.imgUrl;
      this.dialogFormVisible = false;
      let {data:res} = await this.$axios.post("/alleviation/add", {
        id: this.formAdd.id,
        create_time: this.formAdd.create_time,
        title: this.formAdd.title,
        content: this.formAdd.content,
        cover: this.formAdd.cover,
      });
      // console.log(res);
      if (res.code == 200) {
        this.$message.success("添加成功");
      } else {
        this.$message.error("添加失败");
      }
      this.getList(this.pageSize, this.pageNum);
    },
    // 发送编辑的数据到后台
    async onUpdateUser() {
      this.dialogFormVisibleInfo = false;
      let {data:res} = await this.$axios.post("/alleviation/update", {
        title: this.fromUpdateUser.title,
        type: this.fromUpdateUser.type,
        content: this.fromUpdateUser.content,
        id: this.fromUpdateUser.id,
      });
      if (res.code == 200) {
        this.$message.success("修改成功");
      } else {
        this.$message.error("修改失败");
      }
      this.getList(this.pageSize, this.pageNum)
    },
    // 获取当前点击编辑的id
    userFrom(e) {
      this.fromUpdateUser.id = e;
      this.dialogFormVisibleInfo = true;
    },
    // 获取点击删除当前的id
    async removeUser(e) {
      let {data:res} = await this.$axios.get("/alleviation/delete", {
        params:{
          id: e,
        }
      });
     if(res.status == 200){
        this.$message({
        message:'删除成功',
        type:'succese'
      })
     }
     if (res.code == 200) {
        this.$message.success("修改成功");
      } else {
        this.$message.error("修改失败");
      }
      // console.log(data);
      this.getList(this.pageSize, this.pageNum);
    },

     // 点击之后弹出更多搜索
    addSearch() {
      if(this.Search == true){
        this.Search = false
      }else{
        this.Search = true
      }
    },
    nowTime() {
      //获取当前时间
      let now = new Date();
      let _month =
        10 > now.getMonth() + 1
          ? "0" + (now.getMonth() + 1)
          : now.getMonth() + 1;
      let _day = 10 > now.getDate() ? "0" + now.getDate() : now.getDate();
      let _hour = 10 > now.getHours() ? "0" + now.getHours() : now.getHours();
      let _minute =
        10 > now.getMinutes() ? "0" + now.getMinutes() : now.getMinutes();
      let _second =
        10 > now.getSeconds() ? "0" + now.getSeconds() : now.getSeconds();
      return (
        now.getFullYear() +
        "-" +
        _month +
        "-" +
        _day +
        " " +
        _hour +
        ":" +
        _minute +
        ":" +
        _second
      );
    },
  },
  created() {
    this.getList(this.pageSize, this.pageNum);
  },
};
</script>

<style lang="scss">
.btn {
  margin: 20px 0px 0px 20px !important;
}
.block {
  margin: 15px;
}
.el-form-item {
  margin: 0px 30px 0px 0px;
}
</style>